﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Drawing;
using System.Data;
using System.Linq;
using System.Text;
using System.Windows.Forms;

namespace Battleship
{
    public partial class Region : UserControl
    {
        private event System.EventHandler onClickEvent;
        public Container[,] buttons;
        private ContainerManager containerManager;
        private int depth;
        private int itemSize = 37;

        //Define colors

        public Region(ContainerManager containerManager, int depth, int containerSize, EventHandler onClickEvent)
        {
            InitializeComponent();

            this.containerManager = containerManager;
            this.onClickEvent = onClickEvent;

            this.Width = containerManager.width * itemSize + (containerManager.width + 1) * itemSize / 8;
            this.Height = containerManager.height * itemSize + (containerManager.height + 1) * itemSize / 8;

            buttons = new Container[containerManager.width, containerManager.height];
            this.depth = depth;

            drawContainer();
        }

        private void drawContainer()
        {
            for (int i = 0; i < containerManager.height; i++)
            {
                for (int j = 0; j < containerManager.width; j++)
                {
                    Position position = new Position(j, i, depth);

                    ContainerModel container = containerManager.get(position);
                    Container button = null;

                    if (container == null)
                    {
                        button = new Container(itemSize);
                    }
                    else
                    {
                        button = new Container(container, itemSize);
                    }

                    int ii = 7 - i;
                    button.Size = new Size(itemSize, itemSize);
                    int width = (j + 1) * itemSize / 8 + j * itemSize;
                    int height = this.Height - ((i + 1) * itemSize / 8 + i * itemSize) - button.Height;
                    
                    button.Location = new Point(width, height);
                    button.Click += onClickEvent;
                    this.Controls.Add(button);
                    buttons[j, i] = button;

                    button.Tag = position;
                }
            }
        }
    }
}